#include "jiyifanpai.h"
#include "memory.h"

jiyifanpai::jiyifanpai(){
initMap();
}
void jiyifanpai::initMap(){
     int MMap[4][4]={
         {1,8,2,3},
         {4,6,1,5},
         {8,2,6,7},
         {7,5,3,4}
     };
      memcpy(&map[0][0],&MMap[0][0],sizeof(int)*16);

 }

void jiyifanpai::drawMap(QPainter* pDraw){
     QString imgURL;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){

                    switch(map[i][j]){
                    case 0:
                        imgURL="://jiyi/空白.jpeg";
                    break;
                    case 1:
                        imgURL="://jiyi/1.jpeg";
                        break;
                    case 2:
                        imgURL="://jiyi/2.jpeg";
                        break;
                    case 3:
                        imgURL="://jiyi/3.jpeg";
                        break;
                    case 4:
                        imgURL="://jiyi/4.jpeg";
                        break;
                    case 5:
                        imgURL="://jiyi/5.jpeg";
                        break;
                    case 6:
                        imgURL="://jiyi/6.jpeg";
                        break;
                    case 7:
                        imgURL="://jiyi/7.jpeg";
                        break;
                    case 8:
                        imgURL="://jiyi/8.jpeg";
                        break;
                    }
                    QImage img(imgURL);
                    pDraw->drawImage(QRect(j*img.width(),i*img.height(),img.width(),img.height()),img);
                }
            }



        }


void jiyifanpai::mousePressEvent(QMouseEvent *event){

    if(event->button()==Qt::LeftButton){
        int x = event->pos().x()/BLOCK_WIDTH;
        int y =event->pos().y()/BLOCK_HEIGHT;
        time++;
        if(time==1){
            x1=x;
            y1=y;
        }
        else if(time==2){
            x2=x;
            y2=y;
            time=0;
            if(y1==y2 && x2==x1){
                QMessageBox::information(NULL, "Tips", "clicked twice", QMessageBox::Ok);

            }
            else if(map[y1][x1]==map[y2][x2]&&(y1!=y2 )&& (x2!=x1)){
                map[y1][x1]=0;
                map[y2][x2]=0;
            }
        }
    }
}


bool jiyifanpai::gameOver(){
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            if(map[i][j]!=0){
                return false;
            }
        }
    }
return true;
}
